<?php

namespace app\base\command;


use app\base\model\RedianData;
use app\base\model\RedianTask;
use app\base\service\TaskService;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
/**
 * Heygen视频生成状态查询任务
 * Class SmsNum
 * @package app\common\command
 */
class HeygenRetrieve extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('xbase:heygenRetrieve');
        $this->setDescription('HeygenRetrieve');
    }

    protected function execute(Input $input, Output $output)
    {
        $queue_data = $this->queue->data;
        $task_id = $queue_data['task_id'];
        $task_run_log_id = $queue_data['task_run_log_id'];
        $task = RedianTask::mQuery()->where([ "id"=>$task_id])->find();
        $total = RedianData::mQuery()->where(['video_status'=>2,'task_id'=>$task['id'],'task_run_log_id'=>$task_run_log_id])->whereNotNull("video_id")->count();
        $this->setQueueProgress("开始查询视频生成状态{$total}", 0);
        $i = 0;
        while ($i<$total+1){
            $this->setQueueProgress("第{$i}次查询视频生成状态", 0);
            $res = TaskService::instance()->videoStatus($task);
//            if(empty($res)){
//                if($task['is_auto_fb']==1){
//                    RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>6]);
//                }else{
//                    RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7]);
//                }
//
//                break;
//            }
            $i++;
            sleep(1200);
        }
        $num = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'task_run_log_id'=>$task_run_log_id])->whereIn('video_status','1,2')->count();
        if($num==0){
            $s = $this->app->db->name("redian_data")->where(['task_id'=>$task_id,'audio_status'=>3,'video_status'=>3,'task_run_log_id'=>$task_run_log_id])->count();
            if($s>0){
                $is_auto_fb = $this->app->db->name("redian_task")->where(['id'=>$task_id])->value('is_auto_fb');
                if($is_auto_fb==1){
                    $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                        'progress'=>6
                    ]);
                    if($task_run_log_id){
                        $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>6]);
                    }
                    if($task['type']==1){
                        $this->app->db->name("redian_push_schedule")->where(['task_id'=>$task_id])->update(['status'=>1]);
                    }else{
                        //创建推送数据到设备任务
                        sysqueue($task['title']."推送数据到设备任务", "xbase:TaskPush",  10, $data = [
                            'task_id'=>$task_id,'task_run_log_id'=>$task_run_log_id
                        ], $rscript = 1, $loops = 0);
                    }


                }else{
                    $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                        'progress'=>7
                    ]);
                    if($task_run_log_id){
                        $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['progress'=>7]);
                    }
                }

            }else{
                $this->app->db->name("redian_task")->where(['id'=>$task_id])->update([
                    'error_msg'=>"视频生成失败",
                    'error_status'=>5,'status'=>1
                ]);
                if($task_run_log_id){
                    $this->app->db->name("task_run_log")->where(["id"=>$task_run_log_id])->update(['result'=>"视频生成失败",'error_status'=>5]);
                }
            }
        }

       // $this->app->db->name("redian_push_schedule")->where(['task_id'=>$task_id])->update(['status'=>1]);
        $this->setQueueSuccess("任务完成");


    }



}
